package com.eshopkeeper.core.util.yunzhi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class JsonUtil {
    public static final Logger logger = LogManager.getLogger(JsonUtil.class);

    /**
     * 从一个JSON 对象字符格式中得到一个java对象 说明：Bean的无参构造函数一定要写, 否则会报: 
     * net.sf.json.JSONException: java.lang.NoSuchMethodException 
     *  
     * @param jsonString 
     * @param pojoCalss 
     * @return 
     */  
    public static Object getObjectFromJsonString(String jsonString,  
            Class pojoCalss) {  
        Object pojo;   
        pojo = JSON.parseObject(jsonString,pojoCalss);
        return pojo;  
    }  
  
    /** 
     * 将java对象转换成json字符串 
     *  
     * @param javaObj 
     * @return 
     */  
    public static String getJsonStringFromObject(Object javaObj) {  
        return JSON.toJSONString(javaObj);  
    }  
  
    /** 
     * 从json HASH表达式中获取一个map 
     *  
     * @param jsonString 
     * @return 
     */  
    @SuppressWarnings("unchecked")  
    public static Map getMapFromJsonString(String jsonString) {  
        JSONObject jsonObject = JSON.parseObject(jsonString);
        Iterator keyIter = jsonObject.keySet().iterator();
        String key;  
        Object value;  
        Map valueMap = new HashMap();  
        while (keyIter.hasNext()) {  
            key = (String) keyIter.next();  
            value = jsonObject.get(key);  
            valueMap.put(key, value);  
        }  
        return valueMap;  
    }  
  
    public static Map<String,Object> toMap(String jsonString) {  
    	JSONObject jsonObject = JSON.parseObject(jsonString);
        Iterator keyIter = jsonObject.keySet().iterator();
        String key;  
        Object value;  
        Map<String,Object> valueMap = new HashMap<>();  
        while (keyIter.hasNext()) {  
            key = (String) keyIter.next();  
            value = jsonObject.get(key);  
            valueMap.put(key, value);  
        }  
        return valueMap;  
    }
    
    /** 
     * 从Map对象得到Json字串 
     *  
     * @param map 
     * @return 
     */  
    public static String getJsonStringFromMap(Map map) {  
    	return JSON.toJSONString(map);
    }  
  
    /** 
     * 从json字串中得到相应java数组 
     *  
     * @param jsonString 
     *            like "[\"李斯\",100]" 
     * @return 
     */  
    public static Object[] getObjectArrayFromJsonString(String jsonString) {  
        return JSON.parseArray(jsonString).toArray();  
    }  
  
    /** 
     * 将list转换成Array 
     *  
     * @param list 
     * @return 
     */  
//    public static Object[] getObjectArrayFromList(List list) {  
//        return JSON.p;  
//    }  
  
    /** 
     * 用JSONStringer构造一个JsonString 
     *  
     * @param m 
     * @return 
     */  
    public static String buildJsonString(Map m) {  
//        JSONStringer stringer = new JSONStringer();  
//        stringer.object();  
//        for (Object key : m.keySet()) {  
//            stringer.key((String) key)  
//                .value((String)m.get(key));  
//        }  
//        stringer.key("phone");  
//        //begin nesting a array  
//        stringer.array();  
//        stringer.value("13998098000");  
//        stringer.value("8765432");  
//        //nestring object in array  
//        stringer.object();  
//        stringer.key("ppcall");  
//        stringer.value(53881);   
//        stringer.endObject();  
//        stringer.value("13990980980");  
//        //end nesting a array  
//        stringer.endArray();  
//          
//        stringer.endObject();  
//        return stringer.toString();  
    	return JSON.toJSONString(m);
    }  
  
    public static void printMap(Map map) {  
        for (Object key : map.keySet()) {  
            System.out.println(key + ":" + map.get(key));  
        }  
    }  
  
//    public static void main(String[] args) {  
//        Map m = new HashMap() {  
//            {  
//                put("JSon", "HelloWorld");  
//                put("Flex", "Ok");  
//            }  
//        };  
//        System.out.println(buildJsonString(m));  
//        System.out  
//                .println(new JSONStringer().object().key("JSON").value(  
//                        "Hello, World!").key("Flex").value("OK").endObject()  
//                        .toString());  
//    } 
    /**
     * 将json数组转化为list类型
     * 例子：[{"ZDMC":"申请部门"},{"ZDMC":"广西分公司-南宁办共同1","ZDSZ":"空调1"}]转化为list-Map集合
     * @param object
     * @return
     */
//    public static List<Map<String, Object>> toList(Object object)  
//    {  
//        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
//        JSONArray jsonArray = JSONArray.fromObject(object);  
//        for (Object obj : jsonArray)  
//        {  
//            JSONObject jsonObject = (JSONObject) obj;  
//            Map<String, Object> map = new HashMap<String, Object>();  
//            Iterator<?> it = jsonObject.keys();  
//            while (it.hasNext())  
//            {  
//                String key = (String) it.next();  
//                Object value = jsonObject.get(key);  
//                map.put((String) key, value);  
//            }  
//            list.add(map);  
//        }  
//        return list;  
//    }  
}  